package com.hospital.pet.love.lovepethospital.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.pet.love.lovepethospital.domain.Employee;
import com.hospital.pet.love.lovepethospital.domain.PetInfo;
import com.hospital.pet.love.lovepethospital.domain.Register;
import com.hospital.pet.love.lovepethospital.dto.register.AddRegisterDto;
import com.hospital.pet.love.lovepethospital.dto.register.GetRegisterDto;
import com.hospital.pet.love.lovepethospital.exp.ShawCustomException;
import com.hospital.pet.love.lovepethospital.mapper.EmployeeMapper;
import com.hospital.pet.love.lovepethospital.mapper.PetInfoMapper;
import com.hospital.pet.love.lovepethospital.resp.ResultCodeEnum;
import com.hospital.pet.love.lovepethospital.service.RegisterService;
import com.hospital.pet.love.lovepethospital.mapper.RegisterMapper;
import com.hospital.pet.love.lovepethospital.util.AuthenticationContextUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author RoselleShaw
 * @description 针对表【register】的数据库操作Service实现
 * @createDate 2024-04-26 11:11:47
 */
@Service
public class RegisterServiceImpl extends ServiceImpl<RegisterMapper, Register>
        implements RegisterService {

    @Resource
    private RegisterMapper registerMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private PetInfoMapper petInfoMapper;

    @Override
    public void save(AddRegisterDto addRegisterDto) {
        if (employeeMapper.selectOne(new LambdaQueryWrapper<Employee>()
                .eq(Employee::getEmployeeId, addRegisterDto.getDoctorId())
                .eq(Employee::getJob, "医生")
        ) == null) {
            throw new ShawCustomException(ResultCodeEnum.DOCTOR_NOT_EXIST);
        }
        if (petInfoMapper.selectById(addRegisterDto.getPetId()) == null) {
            throw new ShawCustomException(ResultCodeEnum.PET_NOT_EXIST);
        }
        Register register = new Register();
        register.setRegisterId(addRegisterDto.getRegisterId());
        register.setDoctorId(addRegisterDto.getDoctorId());
        register.setPetId(addRegisterDto.getPetId());
        register.setOrderDate(addRegisterDto.getOrderDate());
        register.setState(0);
        if (register.getRegisterId() != null) {
            if (registerMapper.selectById(register.getRegisterId()) == null) {
                throw new ShawCustomException(ResultCodeEnum.REGISTER_NOT_EXIST);
            }
            registerMapper.updateById(register);
        } else {
            registerMapper.insert(register);
        }
    }

    @Override
    public List<Register> getByPhone(GetRegisterDto getRegisterDto) {
        List<Register> registers = new ArrayList<>();
        String phone = AuthenticationContextUtil.get().getPhone();
        LambdaQueryWrapper<PetInfo> petInfoWrapper = new LambdaQueryWrapper<>();
        if (!StrUtil.isEmpty(getRegisterDto.getPetName())) {
            petInfoWrapper.eq(PetInfo::getName, getRegisterDto.getPetName());
        }
        List<PetInfo> pets = petInfoMapper.selectList(
                petInfoWrapper.eq(PetInfo::getPhone, phone)
        );
        Integer doctorId = null;
        if (!StrUtil.isEmpty(getRegisterDto.getDoctorName())) {
            Employee doctor = employeeMapper.selectOne(
                    new LambdaQueryWrapper<Employee>()
                            .eq(Employee::getJob, "医生")
                            .eq(Employee::getEmployeeName, getRegisterDto.getDoctorName())
            );
            if (doctor == null) {
                return registers;
            }
            doctorId = doctor.getEmployeeId();
        }
        if (!CollectionUtils.isEmpty(pets)) {
            LambdaQueryWrapper<Register> wrapper = new LambdaQueryWrapper<>();
            if (getRegisterDto.getStart() != null) {
                wrapper.ge(Register::getOrderDate, getRegisterDto.getStart());
            }
            if (getRegisterDto.getEnd() != null) {
                wrapper.le(Register::getOrderDate, getRegisterDto.getEnd());
            }
            if (doctorId != null) {
                wrapper.eq(Register::getDoctorId, doctorId);
            }
            if (getRegisterDto.getState() != null) {
                if (getRegisterDto.getState() != 1 && getRegisterDto.getState() != 0) {
                    throw new ShawCustomException(ResultCodeEnum.REGISTER_STATE_ERROR);
                }
                wrapper.eq(Register::getState, getRegisterDto.getState());
            }
            pets.forEach(pet -> registers.addAll(registerMapper.selectList(
                    wrapper.eq(Register::getPetId, pet.getPetId())
            )));
        }
        return registers;
    }
}




